--[[	$Id: miniMap.lua 3282 2013-05-22 10:11:26Z sdkyron@gmail.com $	]]

--[[
Categories:
1 - LE_LFG_CATEGORY_LFD (Dungeon finder)
2 - LE_LFG_CATEGORY_LFR (Raid browser)
3 - LE_LFG_CATEGORY_RF (Raid finder)
4 - LE_LFG_CATEGORY_SCENARIO (Scenario finder)
--]]

local _, caelMaps = ...

caelMaps.minimap = caelMaps.createModule("Minimap")

for _, object in pairs({
		BattlegroundShine,
		GameTimeFrame,
		MinimapBorder,
		MinimapZoomIn,
		MinimapZoomOut,
		MinimapNorthTag,
		MinimapBorderTop,
		MiniMapWorldMapButton,
		MinimapZoneTextButton,
		MiniMapTrackingBackground,
		MiniMapTrackingIconOverlay,
		MiniMapTrackingButtonBorder,
		QueueStatusMinimapButtonBorder,
		QueueStatusMinimapButtonGroupSize,
}) do
	if object:GetObjectType() == "Texture" then
		object:SetTexture(nil)
	else
		object:Hide()
	end
end

local mode, category
local isGuildGroup = isGuildGroup

local GetMode = function()
	for i = 1, NUM_LE_LFG_CATEGORYS do
		mode = GetLFGMode(i)
		if mode then
			return mode, i
		end
	end
end

local HideDifficultyFrame = function()
    GuildInstanceDifficulty:EnableMouse(false)
    GuildInstanceDifficulty:SetAlpha(0)

    MiniMapInstanceDifficulty:EnableMouse(false)
    MiniMapInstanceDifficulty:SetAlpha(0)
end

local GetDifficultyText = function()
    local _, _, difficultyIndex, _, maxPlayers = GetInstanceInfo()

    local instanceText = ""

    local isHeroicIndex = {
        [DIFFICULTY_DUNGEON_HEROIC] = true,
        [DIFFICULTY_DUNGEON_CHALLENGE] = true,
        [DIFFICULTY_RAID10_HEROIC] = true,
        [DIFFICULTY_RAID25_HEROIC] = true,
    }

	mode, category = GetMode()

    if IsInInstance() then
		if mode then
			if category == 1 then
				instanceText = "|cffffffffLFD|r"
			elseif category == 3 then
				instanceText = "|cffffffffLFR|r"
			elseif category == 4 then
				instanceText = "|cffffffffSCN|r"
			else
				print("miniMap.lua Category: "..category)
			end
		end

		if isHeroicIndex[difficultyIndex] then
			instanceText = format("|cffffffffHC %s|r", instanceText)
		end

		if isGuildGroup or GuildInstanceDifficulty:IsShown() then
			instanceText = format("|cffffffffGUILD %s|r", instanceText)
		end
    end

    return instanceText
end

Minimap.InstanceText = caelPanel3:CreateFontString(nil, "OVERLAY")
Minimap.InstanceText:SetFont(caelMedia.fonts.CUSTOM_NUMBERFONT, 10)
Minimap.InstanceText:SetPoint("TOP", Minimap, 0, caelLib.scale(-5))
Minimap.InstanceText:SetTextColor(1, 1, 1)
Minimap.InstanceText:SetAlpha(0)

caelMaps.minimap:RegisterEvent("PLAYER_ENTERING_WORLD")
caelMaps.minimap:SetScript("OnEvent", function(self)
	Minimap:EnableMouse(true)
	Minimap:EnableMouseWheel(true)
	Minimap:SetScript("OnMouseWheel", function(Minimap, direction)
		if direction > 0 then
			Minimap_ZoomIn()
		else
			Minimap_ZoomOut()
		end
	end)

	Minimap:ClearAllPoints()
	Minimap:SetParent(caelPanel3)
	Minimap:SetFrameLevel(caelPanel3:GetFrameLevel() - 1)
	Minimap:SetPoint("CENTER")
	Minimap:SetSize(caelPanel3:GetWidth() - caelLib.scale(5), caelPanel3:GetHeight() - caelLib.scale(5))

	Minimap:SetMaskTexture(caelMedia.files.bgFile)
	Minimap:SetBlipTexture([=[Interface\Addons\caelMedia\miscellaneous\charmed.tga]=])

	MinimapCluster:EnableMouse(false)

	QueueStatusMinimapButton:SetParent(Minimap)
	QueueStatusMinimapButton:SetFrameStrata("HIGH")
	QueueStatusMinimapButton:ClearAllPoints()
	QueueStatusMinimapButton:SetPoint("TOPRIGHT")
	QueueStatusMinimapButton:SetHighlightTexture(nil)
	QueueStatusMinimapButton:SetAlpha(0)
	QueueStatusMinimapButton:HookScript("OnEnter", function() securecall("UIFrameFadeIn", QueueStatusMinimapButton, 0.235, 0, 1) end)
	QueueStatusMinimapButton:HookScript("OnLeave", function() securecall("UIFrameFadeOut",QueueStatusMinimapButton, 0.235, 1, 0) end)

	MiniMapTracking:SetParent(Minimap)
	MiniMapTracking:ClearAllPoints()
	MiniMapTracking:SetPoint("TOPLEFT")
	MiniMapTracking:SetAlpha(0)

	MiniMapTrackingButton:SetHighlightTexture(nil)
	MiniMapTrackingButton:HookScript("OnEnter", function() securecall("UIFrameFadeIn", MiniMapTracking, 0.235, 0, 1) end)
	MiniMapTrackingButton:HookScript("OnLeave", function() securecall("UIFrameFadeOut", MiniMapTracking, 0.235, 1, 0) end)

	DurabilityFrame:UnregisterAllEvents()
	DurabilityFrame:SetAlpha(0)

	MiniMapMailFrame:UnregisterAllEvents()

	hooksecurefunc(GuildInstanceDifficulty, "Show", function()
		isGuildGroup = true
		HideDifficultyFrame()
	end)

	hooksecurefunc(GuildInstanceDifficulty, "Hide", function()
		isGuildGroup = false
	end)

	hooksecurefunc(MiniMapInstanceDifficulty, "Show", function()
		HideDifficultyFrame()
	end)

	GuildInstanceDifficulty:HookScript("OnEvent", function(self)
		if (self:IsShown()) then
			isGuildGroup = true
		else
			isGuildGroup = false
		end

		Minimap.InstanceText:SetText(GetDifficultyText())
	end)

	MiniMapInstanceDifficulty:HookScript("OnEvent", function(self)
		Minimap.InstanceText:SetText(GetDifficultyText())
	end)

	Minimap:HookScript("OnEnter", function(self)
		securecall("UIFrameFadeIn", self.InstanceText, 0.235, 0, 1)
	end)

	Minimap:HookScript("OnLeave", function(self)
		securecall("UIFrameFadeOut", self.InstanceText, 0.235, 1, 0)
	end)

	self:UnregisterEvent("PLAYER_ENTERING_WORLD")
end)